package com.hanyxp.config;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Logger;

import com.hanyxp.utile.UtilTools;

public class Properties {
	private PropertiesConfiguration propertiesConfig;
	private static Logger logger = Logger.getLogger(Properties.class);
	
	/**
	 * 通过资源文件构造
	 * @param String properties文件名
	 */
	public Properties(String configFile){
		try {
			this.propertiesConfig = new PropertiesConfiguration(configFile);
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
	}
	
	/** 
	 * 获得当前语言的配置文件
	 * @param req
	 */
	public Properties(HttpServletRequest req){
		String curLang  = UtilTools.getAppLang(req);
		try {
			if(curLang!=null && curLang.equalsIgnoreCase(ExlAttribute.I18N_EN)){
				this.propertiesConfig = new PropertiesConfiguration(ExlAttribute.I18N_EN_FILE);
			}else{
				this.propertiesConfig = new PropertiesConfiguration(ExlAttribute.I18N_ZH_FILE);
			}
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
	}
	/**
	 * default construtor<br />
	 * 默认读取属性中的资源文件<br />
	 * @see {@link ExlAttribute}
	 */
	public Properties(){
		try {
			this.propertiesConfig = new PropertiesConfiguration(ExlAttribute.CONFIG_FILE);
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获得key对应的值, key不存在则返回<b>null</b>
	 * @param String key
	 * @return value 
	 */
	public String getString(String key){
		String value = this.propertiesConfig.getString(key);
		logger.info(value);
		return value;
	}
	public String lookup(String key){
		return this.getString(key);
	}
	/**
	 * 获得key对应的值, key不存在则返回<b>0</b>
	 * @param String key
	 * @return value
	 */
	public int getInt(String key){
		int value = 0;
		try{
			value = this.propertiesConfig.getInt(key);
		}catch (Exception e) {
			e.printStackTrace();
			logger.error("getInt err");
		}
		logger.info(value);
		
		return value;
	}
	/**
	 * 获得key对应的值, key不存在则返回空<b>String[]</b>
	 * @param String key
	 * @return values[]
	 */
	public String[] getStringArray(String key){
		String[] value = this.propertiesConfig.getStringArray(key);
		logger.info(value);
		return  value;
	}

}
